package dfs.island;

/**
 * @Description 695. 岛屿的最大面积
 * @Author Firenut
 * @Date 2023-02-09 23:51
 */
public class T695_maxAreaOfIsland {
    public int maxAreaOfIsland(int[][] grid) {
        int max = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                int ans = dfs(grid, i, j);
                max = Math.max(ans, max);
            }
        }
        return max;
    }

    int dfs(int[][] grid, int x, int y) {
        if (!(x >= 0 && x < grid.length && y >= 0 && y < grid[0].length)) {
            return 0;
        }
        if (grid[x][y] != 1) {
            return 0;
        }
        grid[x][y] = 2;
        int ans = 1 + dfs(grid, x - 1, y) + dfs(grid, x + 1, y) + dfs(grid, x, y - 1) + dfs(grid, x, y + 1);
        return ans;
    }
}
